home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / WINER.ZIP / CHAP8-2.BAS < prev    next >
BASIC Source File  |  1992-05-13  |  1KB  |  45 lines

  1. '*********** CHAP8-2.BAS - simple indexed bubble sort
  2.  
  3. 'Copyright (c) 1992 Ethan Winer
  4.  
  5. DEFINT A-Z
  6. DECLARE SUB BubbleISort (Array$(), Index())
  7.  
  8. CONST NumItems% = 20
  9. CONST False% = 0
  10. CONST True% = -1
  11.  
  12. DIM Array$(1 TO NumItems%)  'this holds the string data
  13. DIM Ndx(1 TO NumItems%)     'this holds the index
  14.  
  15. FOR X = 1 TO NumItems%
  16.   READ Array$(X)            'read the string data
  17.   Ndx(X) = X                'initialize the index array
  18. NEXT
  19.  
  20. CALL BubbleISort(Array$(), Ndx())
  21.  
  22. CLS
  23. FOR X = 1 TO NumItems%
  24.   PRINT Array$(Ndx(X))      'print based on the index
  25. NEXT
  26.  
  27. DATA Zorba, Cathy, Barbara, Kathy, Josephine
  28. DATA Joseph, Joe, Peter, Arnold, Glen
  29. DATA Ralph, Elli, Lucky, Rocky, Louis
  30. DATA Paula, Paul, Mary lou, Marilyn, Keith
  31.  
  32. SUB BubbleISort (Array$(), Index()) STATIC
  33.  
  34. DO
  35.   OutOfOrder = False%                 'assume it's sorted
  36.   FOR X = 1 TO UBOUND(Array$) - 1
  37.     IF Array$(Index(X)) > Array$(Index(X + 1)) THEN
  38.       SWAP Index(X), Index(X + 1)     'if we had to swap
  39.       OutOfOrder% = True%             'we're not done yet
  40.     END IF
  41.   NEXT
  42. LOOP WHILE OutOfOrder%
  43.  
  44. END SUB
  45.